<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" isELIgnored="false" %>
  <script type="text/javascript">


      $(function(){
          $("#bannerdatagrid").datagrid({
              url:'${pageContext.request.contextPath}/banner/findAll',
              remoteSort:false,
              toolbar:'#bannertoolbar',//工具栏
              pagination:true,
              fit:true,
              /*pagePosition:'bottom',*/
              pageSize:5,//每页显示的条数  要求书写的值必须是pageList中的一个元素
              pageList:[5,25,50,100,300],//定义下拉列表中的页码
              fitColumns:true,
              onLoadSuccess:function(){
                  $(".btn").linkbutton({
                      plain:true
                  })
              },

              columns:[[
                  {field:'CK',align:'center',checkbox:true},
                  {field:'id',title:'图片编号',width:200,sortable:true,align:'center'},
                  {field:'path',title:'路径',width:200,sortable:true,align:'center'},
                  {field:'status',title:'状态',width:200,sortable:true,align:'center'},
                  {field:'show',title:'展示',width:200,sortable:true,align:'center',formatter:function(value,row,index){
                        return "<img src='${pageContext.request.contextPath}/"+row.path+"' style='width:150px;height:150px'>"
                      }},
                  {title:"操作",field:"options",width:100,
                      formatter:function(value,row,index){
                          return "<a href='javascript:;' onclick='updateBanner("+JSON.stringify(row)+")' class='btn' data-options=\"iconCls:'icon-edit',\">使用/取消</a>"
                              +"<a data-options=\"iconCls:'icon-edit'\" onClick=\"deleteBanner('"+ row.id +"');\" href='javascript:;' class='btn'>删除</a>";
                      }}
              ]]
          })


      });
      //删除轮播图
      function deleteBanner(id){
          $.messager.confirm('删除提示','确定要删除吗？？？',function(r){
              if(r){
                  $.post("${pageContext.request.contextPath}/banner/delete",{"id":id},function(){
                      $("#bannerdatagrid").datagrid('reload');
                  })
              }
          })
      }

      //批量删除
      function deleteSome(){
          var rows=$("#bannerdatagrid").datagrid("getSelections");
          if(rows.length>0){
              $.messager.confirm('删除提示','确定要删除吗？？',function(r){
                  if(r){
                      var ids=[];
                      $.each(rows,function(index,row){
                          ids.push(row.id);
                      });
                      $.ajax({
                          url:"${pageContext.request.contextPath}/emp/deleteSome",
                          data:{id:ids},
                          dataType:"JSON",
                          method:'post',
                          traditional:true,
                          success:function(result){
                              $("#bannerdatagrid").datagrid('reload');
                          }
                      })
                  }
              })
          }else{
              alert("至少选择一项！！");
          }
      }

      //打开添加轮播图面板
      function openaddBanner(){
          $("#addBannerDialog").dialog({
              title:'添加轮播图',
              width:450,
              height:450,
              iconCls:'icon-man',
              href:'${pageContext.request.contextPath}/back/banner/addBanner.jsp',
              buttons:[{
                  text:'添加',
                  iconCls:'icon-add',
                  handler:addBanner
              },{
                  text:'关闭',
                  iconCls:'icon-cancel',
                  handler:function(){
                      $("#addBannerDialog").dialog('close');
                  }
              }]
          })
      }

      //添加banner
      function addBanner(){
          $("#addBannerForm").form({
              url:'${pageContext.request.contextPath}/banner/save',
              onSubmit:function(){
                  return $("#addBannerForm").form('validate');
              },
              success:function(){
                  $.messager.show({
                      title:'提示信息',
                      msg:'添加成功',
                      timeout:5000,
                      showType:'slide'
                  });
                  $("#addBannerDialog").dialog('close');
                  $("#bannerdatagrid").datagrid('reload');
              }
          });
          $("#addBannerForm").form('submit');
      }
      //更新轮播图
      function updateBannerStatus(){
          $("#updateBannerForm").form({
              url:'${pageContext.request.contextPath}/banner/update',
              onSubmit:function(){
                  return $(this).form('validate');
              },
              success:function(result){
                  console.log(result);
                  $.messager.show({
                      title:'提示信息',
                      msg:'更新成功',
                      timeout:5000,
                      showType:'slide'
                  });
                  $("#bannerdatagrid").datagrid('reload');
              },
          });
          $("#updateBannerForm").form('submit');
          $("#updateBannerDialog").dialog('close');
      }

      function updateBanner(row){
          if(row.status==0){
              $.messager.confirm('确认','您确认想要使用该图片吗？',function(r){
                  if(r){

                      $("#updateBannerDialog").dialog({
                          title:'更新轮播图',
                          width:450,
                          height:450,
                          iconCls:'icon-man',
                          href:'${pageContext.request.contextPath}/back/banner/updateBanner.jsp?id='+row.id,
                          buttons:[{
                              text:'更新',
                              iconCls:'icon-add',
                              handler:updateBannerStatus
                          },{
                              text:'关闭',
                              iconCls:'icon-cancel',
                              handler:function(){
                                  $("#addBannerDialog").dialog('close');
                              }
                          }]
                      })
                  }
              });
          }else{
              $.messager.confirm('确认','您确认想要取消使用该图片吗？',function(r){
                  if(r){

                      $("#updateBannerDialog").dialog({
                          title:'更新轮播图',
                          width:450,
                          height:450,
                          iconCls:'icon-man',
                          href:'${pageContext.request.contextPath}/back/banner/updateVipcard.jsp?id='+row.id,
                          buttons:[{
                              text:'更新',
                              iconCls:'icon-add',
                              handler:updateBannerStatus
                          },{
                              text:'关闭',
                              iconCls:'icon-cancel',
                              handler:function(){
                                  $("#addBannerDialog").dialog('close');
                              }
                          }]
                      })
                  }
              });
          }
      }
  </script>
</head>




<table id="bannerdatagrid" ></table>
<div id="bannertoolbar">
  <a href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="openaddBanner();">添加轮播图</a>
  <a href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="deleteSome();">删除选中</a>
</div>

<div id="addBannerDialog"></div>
<div id="updateBannerDialog"></div>

</form>

